import 'package:flutter/material.dart';
import '../usersp/user_sp.dart';
import 'ads_const.dart';
import 'ads_utils.dart';

//正常信息流
class MyAdFeedView extends StatefulWidget {
  final bool isCSJ;

  const MyAdFeedView({super.key, this.isCSJ = false});

  @override
  State<MyAdFeedView> createState() => _FeedViewState();
}

class _FeedViewState extends State<MyAdFeedView> {
  Widget? feedAdList;

  bool _hasCalled = false;

  @override
  void initState() {
    super.initState();
    if (!_hasCalled) {
      _hasCalled = true;
      WidgetsBinding.instance.addPostFrameCallback((_) {
        if (widget.isCSJ) {
          getAdFeedWidgetCSJ();
        } else {
          getAdFeedWidget();
        }
      });
    }
  }

  void getAdFeedWidget() async {
    var isPrivacyAccept = await UserSp.instance.isPrivacyAccept();
    //如果没有同意隐私权限则不能获取
    if (!isPrivacyAccept) {
      return;
    }
    //判断广告开关
    if (!AdsConst.isOpenAds) {
      return;
    }
    feedAdList = await AdsUtils.requestFeedAdList(
      width: 375,
      height: 400,
      count: 1,
    );
    setState(() {});
  }

  void getAdFeedWidgetCSJ() async {
    var isPrivacyAccept = await UserSp.instance.isPrivacyAccept();
    //如果没有同意隐私权限则不能获取
    if (!isPrivacyAccept) {
      return;
    }
    feedAdList = await AdsUtils.requestFeedAdListCSJ(
      width: 375,
      height: 400,
      count: 1,
    );
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      width: double.infinity,
      child: feedAdList,
    );
  }
}
